/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.commdit.service.impl;

import me.peng.commdit.domain.Shopcomment;
import me.peng.commdit.service.mapstruct.CommodityOrderMapper;
import me.peng.domain.config.SysResult;
import me.peng.domain.mapper.commodityCommentMapper;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.commdit.repository.ShopcommentRepository;
import me.peng.commdit.service.ShopcommentService;
import me.peng.commdit.service.dto.ShopcommentDto;
import me.peng.commdit.service.dto.ShopcommentQueryCriteria;
import me.peng.commdit.service.mapstruct.ShopcommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-10-21
**/
@Service
@RequiredArgsConstructor
public class ShopcommentServiceImpl implements ShopcommentService {

    private final ShopcommentRepository shopcommentRepository;
    private final ShopcommentMapper shopcommentMapper;

    @Override
    public Map<String,Object> queryAll(ShopcommentQueryCriteria criteria, Pageable pageable){
        Page<Shopcomment> page = shopcommentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shopcommentMapper::toDto));
    }

    @Override
    public List<ShopcommentDto> queryAll(ShopcommentQueryCriteria criteria){
        return shopcommentMapper.toDto(shopcommentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ShopcommentDto findById(Integer id) {
        Shopcomment shopcomment = shopcommentRepository.findById(id).orElseGet(Shopcomment::new);
        ValidationUtil.isNull(shopcomment.getId(),"Shopcomment","id",id);
        return shopcommentMapper.toDto(shopcomment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopcommentDto create(Shopcomment resources) {
        return shopcommentMapper.toDto(shopcommentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Shopcomment resources) {
        Shopcomment shopcomment = shopcommentRepository.findById(resources.getId()).orElseGet(Shopcomment::new);
        ValidationUtil.isNull( shopcomment.getId(),"Shopcomment","id",resources.getId());
        shopcomment.copy(resources);
        shopcommentRepository.save(shopcomment);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shopcommentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShopcommentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShopcommentDto shopcomment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", shopcomment.getUserid());
            map.put("商品id", shopcomment.getShopid());
            map.put("用户名", shopcomment.getName());
            map.put("内容", shopcomment.getContent());
            map.put("创建时间", shopcomment.getCreatedata());
            map.put("评论对象", shopcomment.getShopobject());
            map.put("订单信息", shopcomment.getOrderinfo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Autowired
    commodityCommentMapper commodityOrderMapper2;

    @Override
    public SysResult postShopComment(Shopcomment comment) {

        System.out.println(comment.getUserid()+"<----------------");
        commodityOrderMapper2.insert(comment);
        return SysResult.success();
    }


}
